using System;
using System.Collections.Generic;
using System.Windows.Threading;
namespace MicroMsg.UI.UserContrl
{
	public static class MMHubTileService
	{
		private const int WaitingPipelineSteps = 3;
		private const int NumberOfSimultaneousAnimations = 1;
		private const bool TrackResurrection = false;
		private static DispatcherTimer Timer;
		private static Random ProbabilisticBehaviorSelector;
		private static List<WeakReference> EnabledImagesPool;
		private static List<WeakReference> FrozenImagesPool;
		private static List<WeakReference> StalledImagesPipeline;
		static MMHubTileService()
		{
			MMHubTileService.Timer = new DispatcherTimer();
			MMHubTileService.ProbabilisticBehaviorSelector = new Random();
			MMHubTileService.EnabledImagesPool = new List<WeakReference>();
			MMHubTileService.FrozenImagesPool = new List<WeakReference>();
			MMHubTileService.StalledImagesPipeline = new List<WeakReference>();
			MMHubTileService.Timer.add_Tick(new EventHandler(MMHubTileService.OnTimerTick));
		}
		private static void RestartTimer()
		{
			if (!MMHubTileService.Timer.get_IsEnabled())
			{
				MMHubTileService.Timer.set_Interval(TimeSpan.FromMilliseconds(2500.0));
				MMHubTileService.Timer.Start();
			}
		}
		internal static void InitializeReference(MMHubTile tile)
		{
			WeakReference tile2 = new WeakReference(tile, false);
			if (tile.IsFrozen)
			{
				MMHubTileService.AddReferenceToFrozenPool(tile2);
			}
			else
			{
				MMHubTileService.AddReferenceToEnabledPool(tile2);
			}
			MMHubTileService.RestartTimer();
		}
		internal static void FinalizeReference(MMHubTile tile)
		{
			WeakReference tile2 = new WeakReference(tile, false);
			MMHubTileService.RemoveReferenceFromEnabledPool(tile2);
			MMHubTileService.RemoveReferenceFromFrozenPool(tile2);
			MMHubTileService.RemoveReferenceFromStalledPipeline(tile2);
		}
		private static void AddReferenceToEnabledPool(WeakReference tile)
		{
			if (!MMHubTileService.ContainsTarget(MMHubTileService.EnabledImagesPool, tile.get_Target()))
			{
				MMHubTileService.EnabledImagesPool.Add(tile);
			}
		}
		private static void AddReferenceToFrozenPool(WeakReference tile)
		{
			if (!MMHubTileService.ContainsTarget(MMHubTileService.FrozenImagesPool, tile.get_Target()))
			{
				MMHubTileService.FrozenImagesPool.Add(tile);
			}
		}
		private static void AddReferenceToStalledPipeline(WeakReference tile)
		{
			if (!MMHubTileService.ContainsTarget(MMHubTileService.StalledImagesPipeline, tile.get_Target()))
			{
				MMHubTileService.StalledImagesPipeline.Add(tile);
			}
		}
		private static void RemoveReferenceFromEnabledPool(WeakReference tile)
		{
			MMHubTileService.RemoveTarget(MMHubTileService.EnabledImagesPool, tile.get_Target());
		}
		private static void RemoveReferenceFromFrozenPool(WeakReference tile)
		{
			MMHubTileService.RemoveTarget(MMHubTileService.FrozenImagesPool, tile.get_Target());
		}
		private static void RemoveReferenceFromStalledPipeline(WeakReference tile)
		{
			MMHubTileService.RemoveTarget(MMHubTileService.StalledImagesPipeline, tile.get_Target());
		}
		private static bool ContainsTarget(List<WeakReference> list, object target)
		{
			for (int i = 0; i < list.get_Count(); i++)
			{
				if (list.get_Item(i).get_Target() == target)
				{
					return true;
				}
			}
			return false;
		}
		private static void RemoveTarget(List<WeakReference> list, object target)
		{
			for (int i = 0; i < list.get_Count(); i++)
			{
				if (list.get_Item(i).get_Target() == target)
				{
					list.RemoveAt(i);
					return;
				}
			}
		}
		private static void OnTimerTick(object sender, EventArgs e)
		{
			MMHubTileService.Timer.Stop();
			for (int i = 0; i < MMHubTileService.StalledImagesPipeline.get_Count(); i++)
			{
				if ((MMHubTileService.StalledImagesPipeline.get_Item(i).get_Target() as MMHubTile)._stallingCounter-- == 0)
				{
					MMHubTileService.AddReferenceToEnabledPool(MMHubTileService.StalledImagesPipeline.get_Item(i));
					MMHubTileService.RemoveReferenceFromStalledPipeline(MMHubTileService.StalledImagesPipeline.get_Item(i));
					i--;
				}
			}
			if (MMHubTileService.EnabledImagesPool.get_Count() > 0)
			{
				for (int j = 0; j < 1; j++)
				{
					int num = MMHubTileService.ProbabilisticBehaviorSelector.Next(MMHubTileService.EnabledImagesPool.get_Count());
					switch ((MMHubTileService.EnabledImagesPool.get_Item(num).get_Target() as MMHubTile).State)
					{
					case ImageState.HeadImg:
						if ((MMHubTileService.EnabledImagesPool.get_Item(num).get_Target() as MMHubTile)._canFlip)
						{
							(MMHubTileService.EnabledImagesPool.get_Item(num).get_Target() as MMHubTile).State = ImageState.MsgShow;
						}
						break;
					case ImageState.MsgShow:
						(MMHubTileService.EnabledImagesPool.get_Item(num).get_Target() as MMHubTile).State = ImageState.HeadImg;
						break;
					}
					(MMHubTileService.EnabledImagesPool.get_Item(num).get_Target() as MMHubTile)._stallingCounter = 3;
					MMHubTileService.AddReferenceToStalledPipeline(MMHubTileService.EnabledImagesPool.get_Item(num));
					MMHubTileService.RemoveReferenceFromEnabledPool(MMHubTileService.EnabledImagesPool.get_Item(num));
				}
			}
			else
			{
				if (MMHubTileService.StalledImagesPipeline.get_Count() == 0)
				{
					return;
				}
			}
			MMHubTileService.Timer.set_Interval(TimeSpan.FromMilliseconds((double)(MMHubTileService.ProbabilisticBehaviorSelector.Next(1, 31) * 100)));
			MMHubTileService.Timer.Start();
		}
		public static void FreezeHubTile(MMHubTile tile)
		{
			WeakReference tile2 = new WeakReference(tile, false);
			MMHubTileService.AddReferenceToFrozenPool(tile2);
			MMHubTileService.RemoveReferenceFromEnabledPool(tile2);
			MMHubTileService.RemoveReferenceFromStalledPipeline(tile2);
		}
		public static void UnfreezeHubTile(MMHubTile tile)
		{
			WeakReference tile2 = new WeakReference(tile, false);
			MMHubTileService.AddReferenceToEnabledPool(tile2);
			MMHubTileService.RemoveReferenceFromFrozenPool(tile2);
			MMHubTileService.RemoveReferenceFromStalledPipeline(tile2);
			MMHubTileService.RestartTimer();
		}
		public static void FreezeGroup(string group)
		{
			for (int i = 0; i < MMHubTileService.EnabledImagesPool.get_Count(); i++)
			{
				if ((MMHubTileService.EnabledImagesPool.get_Item(i).get_Target() as MMHubTile).GroupTag == group)
				{
					(MMHubTileService.EnabledImagesPool.get_Item(i).get_Target() as MMHubTile).IsFrozen = true;
					i--;
				}
			}
			for (int j = 0; j < MMHubTileService.StalledImagesPipeline.get_Count(); j++)
			{
				if ((MMHubTileService.StalledImagesPipeline.get_Item(j).get_Target() as MMHubTile).GroupTag == group)
				{
					(MMHubTileService.StalledImagesPipeline.get_Item(j).get_Target() as MMHubTile).IsFrozen = true;
					j--;
				}
			}
		}
		public static void UnfreezeGroup(string group)
		{
			for (int i = 0; i < MMHubTileService.FrozenImagesPool.get_Count(); i++)
			{
				if ((MMHubTileService.FrozenImagesPool.get_Item(i).get_Target() as MMHubTile).GroupTag == group)
				{
					(MMHubTileService.FrozenImagesPool.get_Item(i).get_Target() as MMHubTile).IsFrozen = false;
					i--;
				}
			}
			MMHubTileService.RestartTimer();
		}
	}
}
